//
//  HWweixingtishiView.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/12/1.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWweixingtishiView.h"
#import "HWTaskCenterViewController.h"


@interface HWweixingtishiView()


@property(strong,nonatomic) UIView *grayView;
//@property(strong,nonatomic) UIView *whiteView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) NSArray *icoArr;
@property(strong,nonatomic) NSArray *listArr;

@property(strong,nonatomic) UIImageView *fenxiangImge;


@end

@implementation HWweixingtishiView

 - (instancetype)initWithFrame:(CGRect)frame  {
     
     if (self  = [super initWithFrame:frame]) {
         // 添加灰色背景
         [self addgrayView];
         
         [self setupUI];
         
         
     }
     return  self;
     
 }

 //  设置UI
 - (void)setupUI {
     
     self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(20, (kScreenH- 222)/2, kScreenW-40, 320)];
     self.fenxiangView.backgroundColor = RGB(255, 255, 255);
     self.fenxiangView.layer.cornerRadius = 12;
     self.fenxiangView.layer.masksToBounds = YES;
     [self addSubview:self.fenxiangView];
     
     
 //    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-120)/2, 10, 120, 120)];
 //    self.fenxiangImge = image1;
 //    [self.fenxiangView addSubview:image1];
 //
     
     
     
     
     
     
     
     UILabel *fenxianlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, kScreenW-40, 28)];
     fenxianlabel.text = @"温馨提示";
     fenxianlabel.font = [UIFont systemFontOfSize:20];
     fenxianlabel.textColor = RGB(68, 68, 68);
     fenxianlabel.textAlignment =  NSTextAlignmentCenter;
     [self.fenxiangView  addSubview:fenxianlabel];
     
     
     
     UILabel *fenxianlabel1 = [[UILabel alloc]initWithFrame:CGRectMake(35, fenxianlabel.bottom+10, kScreenW-70-40, 40)];
     fenxianlabel1.text = @"尊敬的用户您好，您的积分不足无法提供兑换，请先获取积分哦！";
     fenxianlabel1.font = [UIFont systemFontOfSize:14];
     fenxianlabel1.textColor = RGB(68, 68, 68);
     fenxianlabel1.numberOfLines = 2;
     fenxianlabel1.textAlignment =  NSTextAlignmentCenter;
     [self.fenxiangView  addSubview:fenxianlabel1];
     
     
     
     
     UILabel *fenxianlabel2 = [[UILabel alloc]initWithFrame:CGRectMake(35, fenxianlabel1.bottom+15, kScreenW-70-40, 20)];
         fenxianlabel2.text = @"如何获取积分";
         fenxianlabel2.font = [UIFont systemFontOfSize:14];
         fenxianlabel2.textColor = RGB(68, 68, 68);
         fenxianlabel2.numberOfLines = 2;
         fenxianlabel2.textAlignment =  NSTextAlignmentLeft;
        [self.fenxiangView  addSubview:fenxianlabel2];
     
     
     UILabel *fenxianlabel3 = [[UILabel alloc]initWithFrame:CGRectMake(35, fenxianlabel2.bottom+2, kScreenW-70-40, 80)];
           fenxianlabel3.text = @"1.每日签到\n2.会员充值返还\n3.发表优质游戏评论\n4.完成新手任务，每日任务\n5.参与邀请好友活动";
           fenxianlabel3.font = [UIFont systemFontOfSize:12];
           fenxianlabel3.textColor = RGB(136, 136, 136);
           fenxianlabel3.numberOfLines = 6;
           fenxianlabel3.textAlignment =  NSTextAlignmentLeft;
          [self.fenxiangView  addSubview:fenxianlabel3];
       
     
     
         
     
     
     
     
     
     

        UIButton *loginbutt  = [UIButton setButtonWithFrame:CGRectMake(20,fenxianlabel3.bottom+10,kScreenW-80, 44) Title:@"去获取积分" Image:nil Font:15 Color:RGB(255, 255, 255) Target:self Selector:@selector(logtapTap:)];
       //  loginbutt.backgroundColor = RGB(68, 94, 233);
       //  [loginbutt setBackgroundImage:[UIImage imageNamed:@"button_s"] forState:UIControlStateNormal];
     //    loginbutt.layer.shadowColor = [UIColor colorWithRed:26/255.0 green:113/255.0 blue:255/255.0 alpha:0.35].CGColor;
     //    loginbutt.layer.shadowOffset = CGSizeMake(0,0);
     //    loginbutt.layer.shadowOpacity = 1;
     //    loginbutt.layer.shadowRadius = 10;
     //    loginbutt.layer.cornerRadius = 6;
     //    loginbutt.layer.masksToBounds = YES;
     //    loginbutt.clipsToBounds = NO;
     
         loginbutt.backgroundColor = RGB(246, 106, 5);
         loginbutt.layer.masksToBounds = YES;
         loginbutt.layer.cornerRadius = 22;
         [self.fenxiangView addSubview:loginbutt];
     
     
     
     
     
   
     
     
     
     
     

     
     
     
     

 //    self.listArr = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
 //    self.icoArr = @[@"share_w",@"share_q",@"share_qqico",@"share_c"];
 //    CGFloat jianju = (kScreenW - 82-4*50)/3;
 //    // CGFloat  shujianju = (166-100)/3;
 //
 //    for (int i = 0 ; i < self.listArr.count; i++) {
 //
 //
 //        UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(jianju*i+41+i*50, 52, 50, 50) Title:self.listArr[i] Image:self.icoArr[i] Font:13 Color:RGB(51, 51, 51) Target:self Selector:@selector(buttonTap:)];
 //        butt.tag = i;
 //        [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
 //        // butt.backgroundColor = [UIColor redColor];
 //        [self.fenxiangView addSubview:butt];
 //
 //    }
     
 //    UIView *fengeview = [[UIView alloc]initWithFrame:CGRectMake(0,137, kScreenW, 1)];
 //    fengeview.backgroundColor = RGB(215, 215, 215);
 //
 //
 //    [self.fenxiangView addSubview:fengeview];
 //
 //    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 //    button.frame = CGRectMake(0, 138, kScreenW, 38);
 //    [button setTitle:@"保存" forState:UIControlStateNormal];
 //    [button setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
 //    [button addTarget:self action:@selector(quiabuttontap) forControlEvents:UIControlEventTouchUpInside];
 //    button.titleLabel.font = [UIFont systemFontOfSize:14];
 //    [self.fenxiangView addSubview:button];
 //
     
     
     
     
     
     
     
     
     
 }


 - (void)relayoutTheChildControls {
 //    // self.whiteView.backgroundColor = [UIColor redColor];
 //    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 120);
 //    self.grayView.frame = CGRectMake(0, self.whibackvieY+120, kScreenW, kScreenH-self.whibackvieY-118+10);
 //

     
 }






 // 添加灰色View
 -(void)addgrayView {
     
     self.grayView = [[UIView alloc]init];
     self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
     
     self.grayView.backgroundColor =  RGB(51, 51, 51);
     self.grayView.alpha = 0.6;
     [self addSubview:self.grayView];
     
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
     //  tap.delegate = self;
     [self.grayView addGestureRecognizer:tap];
     
     
     
     
 }

 - (void)tapView:(UITapGestureRecognizer *)touch {
     
     [touch.view.superview removeFromSuperview];
     //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
     
     
 }


 //点击buton 按钮

 - (void)buttonTap:(UIButton *)sender {
     
  //   [self.delegate xuazhongTag:(int)sender.tag];
     
     
     
    
     
 }

 #pragma mark ------ 取消
 - (void)quiabuttontap {
     
   //  [self removeFromSuperview];
     
     
    UIImageWriteToSavedPhotosAlbum(self.fenxiangImge.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
     #pragma mark -- <保存到相册>
    
     
     
 }

 -(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
     NSString *msg = nil ;
     if(error){
         msg = @"保存图片失败" ;
         [MBProgressHUD showSuccess:@"保存失败"];
         
     }else{
         msg = @"保存图片成功" ;
            [MBProgressHUD showSuccess:@"保存成功"];
     }
 }



 //-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
 //
 //    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
 //
 //        return NO;
 //    }else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
 //
 //
 //        return NO;
 //    }
 //    return YES;
 //
 //
 //}


 - (void)setUrlStr:(NSString *)urlStr {
     
     //  _urlStr = urlStr;
     
     self.fenxiangImge.image =[self createNonInterpolatedUIImageFormCIImage:[self creatQRcodeWithUrlstring:urlStr] withSize:150];
     
     

     
     
 }

 - (CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
     
     // 1.实例化二维码滤镜
     CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
     // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
     [filter setDefaults];
     // 3.将字符串转换成NSdata
     NSData *data  = [urlString dataUsingEncoding:NSUTF8StringEncoding];
     // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
     [filter setValue:data forKey:@"inputMessage"];
     // 5.生成二维码
     CIImage *outputImage = [filter outputImage];
     return outputImage;
 }
 - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
 {
     CGRect extent = CGRectIntegral(image.extent);
     CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
     
     // 1.创建bitmap;
     size_t width = CGRectGetWidth(extent) * scale;
     size_t height = CGRectGetHeight(extent) * scale;
     CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
     CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
     CIContext *context = [CIContext contextWithOptions:nil];
     CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
     CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
     CGContextScaleCTM(bitmapRef, scale, scale);
     CGContextDrawImage(bitmapRef, extent, bitmapImage);
     
     // 2.保存bitmap到图片
     CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
     CGContextRelease(bitmapRef);
     CGImageRelease(bitmapImage);
     return [UIImage imageWithCGImage:scaledImage];
 }




 - (void)logtapTap:(UIButton *)sender  {
     
     
     
     HWTaskCenterViewController *taskCenter = [[HWTaskCenterViewController  alloc]init];
    [[HWViewUtils getCurrentVC].navigationController  pushViewController:taskCenter animated:YES];
      [self removeFromSuperview];
          
     
//     HWRealNameAuthenticationController *authentication = [[HWRealNameAuthenticationController alloc]init];
//     [[HWViewUtils getCurrentVC].navigationController pushViewController:authentication animated:YES];
//
//     [self removeFromSuperview];
//
     
     
 }

 - (void)quexiao:(UIButton *)sender  {
     
       [self removeFromSuperview];
     
 }


@end
